﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Masood.Utilities.EmailTemplates.Items
{
    //This is generic SharePoint list item implementation. You may need to implement with other list.
    //For instance if you are maintaining customers in Customers list you have to create CustomerSharePointListItem so that you
    //format and customise the value in PopulateDictionary methods or whatever method you want to call it.

    public class SharePointListItem : IItem
    {
        private SPListItem spListItem = null;

        public SharePointListItem(SPListItem spListItem)
        {
            fieldValues = new Dictionary<string, string>();
            this.spListItem = spListItem;
            PopulateDictionary();
        }

        public SPListItem SpListItem
        {
            set
            {
                spListItem = value;
                PopulateDictionary();
            }
        }

        private void PopulateDictionary()
        {
            if (spListItem == null)
                return;

            foreach (SPField field in spListItem.Fields)
            {
                fieldValues.Add(field.Title, spListItem[field.Id].ToString());
            }
        }

        private Dictionary<string, string> fieldValues;
        public Dictionary<string, string> FieldAndValues
        {
            get
            {
                return fieldValues;
            }
            set
            {
                fieldValues = value;
            }
        }
    }
}
